/*
 * Engine TEST
 * danouvrs@gmail.com
 * You have no right on this file
 */

package engine.window;

import javax.media.opengl.*;
import com.jogamp.opengl.util.FPSAnimator;
import javax.media.opengl.awt.GLCanvas;
import java.awt.Frame;



public class GraphicsPane{
    
    private GLProfile glp;
    private GLCapabilities caps;
    private GLCanvas canvas;
    private FPSAnimator animator;
    //private FPS fps;
    
    public GraphicsPane(Frame frame){
        glp = GLProfile.getDefault();
        caps = new GLCapabilities(glp);
        caps.setHardwareAccelerated(true);
        caps.setSampleBuffers(true);
        caps.setNumSamples(8);
        caps.setAlphaBits(1);
        canvas = new GLCanvas(caps);
        
        int[] screenSize = new int[] { getWidth(frame), getHeight(frame)};
        
        //gfx = new Graphics(screenSize,1);
        
        //canvas.addGLEventListener(gfx);
        //this.addKeyListener(keyboard);
        
        if(setFPSAnimator(1000)){
            System.out.println("FPSAnimator initialized");
        }
        else{
            System.err.println("FPSAnimator not initialized");
        }

        //fps = new FPS(animator, frame, VERBOSE);
        //fps.enableFpsInFrameTitle();

        canvas.requestFocus();
    }
    
    public boolean setFPSAnimator(int frameRate){
        animator = new FPSAnimator(canvas, frameRate);
        animator.start();
        
        return true;
    }
    
    public FPSAnimator getFPSAnimator() {
        return animator;
    }
    /*
    public Graphics getGfx() {
        return gfx;
    }
    
    public FPS getFPS() {
        return fps;
    }
     */

    public GLCanvas getGLCanvas() {
        return canvas;
    }
    
    public void FPSAnimatorPause(){
        if(animator.isPaused()){
            animator.resume();
        }else{
            animator.pause();
        }
    }
    public boolean FPSAnimatorStop(){
        if(animator.isPaused()){
            animator.resume();
            FPSAnimatorStop();
        }else{
            animator.stop();
            return true;
        }
        return false;
    }
    
    public int getWidth(Frame fr){
        return fr.getWidth();
    }
    
    public int getHeight(Frame fr){
        return fr.getHeight();
    }
    
    public void setAA(int n){
        if(n > 8)
            n = 8;
        if(n < 0)
            n = 0;
        caps.setNumSamples(n);
        showAA();
    }
    
    public void showAA(){
        System.out.println("AA is " + caps.getNumSamples());
    }

}